/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
*/
package net.redxyz.repost.resource.action.schedule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.ZmlContexts;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.annotation.AnFilterNot;
import org.zhiqim.kernel.extend.HashMapSO;
import org.zhiqim.kernel.extend.MapSO;
import org.zhiqim.kernel.paging.PageBuilder;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.Objects;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.repost.resource.model.AssRepostScheduleModel;
import net.redxyz.repost.service.dao.ItemDao;
import net.redxyz.top.repost.dbo.AssRepostSchedule;
import net.redxyz.top.repost.dbo.AssRepostScheduleTime;

/**
 * 定时上下架
 *
 * @version v7.0.1 @author zouzhigang 2021-7-29 新建与整理
 */
public class RepostScheduleAction implements Action
{
    public void execute(HttpRequest request) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        sessionUser.removeValue("items_Schedule");
    }

    /**
     * 根据条件查询宝贝信息
     *
     * @param request
     * @param q 标题关键字
     */
    @AnIntercept("chkAjax")
    @AnFilterNot
    public String doQuery(HttpRequest request, String q, int page) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String nick = sessionUser.getNick();
        int pageSize = 10;

        // 查询定时列表
        Selector selector = new Selector();
        selector.addMaybe("nick", nick);
        ItemDao.addWhereQ(selector, q);
        selector.addOrderbyDesc("createTime");
        PageResult<AssRepostSchedule> result = ORM.table().page(AssRepostSchedule.class, page, pageSize, selector);
        if (result.size() == 0)
        {
            return ZmlContexts.parseZmlPath(request, "/zview/schedule/presenter/repostScheduleInfo.zml", "result", result);
        }

        HashMap<Long, AssRepostScheduleModel> loopMap = new HashMap<Long, AssRepostScheduleModel>();
        List<AssRepostSchedule> list = result.list();
        long[] numIids = new long[list.size()];
        for (int i = 0; i < list.size(); i++)
        {
            AssRepostSchedule repost = list.get(i);
            numIids[i] = repost.getNumIid();
            AssRepostScheduleModel schedule = new AssRepostScheduleModel();
            Objects.copy(repost, schedule);
            loopMap.put(repost.getNumIid(), schedule);
        }

        // 查找所有loop的loopTime
        Selector sel = new Selector();
        sel.addMaybe("nick", nick);
        sel.addMaybeInLong("numIid", numIids);
        sel.addOrderbyAsc("planTime");
        List<AssRepostScheduleTime> timeList = ORM.table().list(AssRepostScheduleTime.class, sel);

        for (AssRepostScheduleTime time : timeList)
        {
            long numIid = time.getNumIid();
            AssRepostScheduleModel loop = loopMap.get(numIid);
            loop.addTime(time);
        }

        PageResult<AssRepostScheduleModel> nresult = PageBuilder.newResult(result.total(), page, pageSize, new ArrayList<AssRepostScheduleModel>(loopMap.values()));

        MapSO attrMap = new HashMapSO();
        attrMap.put("result", nresult);

        return ZmlContexts.parseZmlPath(request, "/zview/schedule/presenter/repostScheduleInfo.zml", attrMap);
    }

    @AnIntercept("chkAjax")
    public String doDelete(HttpRequest request, long numIid) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String nick = sessionUser.getNick();

        ORM.table().delete(AssRepostSchedule.class, nick, numIid);
        ORM.table().delete(AssRepostScheduleTime.class, new Selector("nick", nick).addMaybe("numIid", numIid));

        return "删除成功";
    }

    @AnIntercept("chkAjax")
    public String doStop(HttpRequest request, long numIid) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String nick = sessionUser.getNick();

        Updater updater = new Updater();
        updater.addField("planStatus", 0);
        updater.addMaybe("nick", nick);
        updater.addMaybe("numIid", numIid);
        ORM.table().update(AssRepostSchedule.class, updater);

        return "暂停成功";
    }

    @AnIntercept("chkAjax")
    public String doStart(HttpRequest request, long numIid) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String nick = sessionUser.getNick();
        Updater updater = new Updater();
        updater.addField("planStatus", 1);
        updater.addMaybe("nick", nick);
        updater.addMaybe("numIid", numIid);
        ORM.table().update(AssRepostSchedule.class, updater);

        return "启动成功";
    }
}
